package com.hnx.admin.aurora.core;

import com.hnx.admin.aurora.core.api.SqlBuilder;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/*
创建人： 倔强的头发
创建时间：2025/7/4
功能描述:

*/
public class SafeSqlBuilder implements SqlBuilder {
    private final StringBuilder sql = new StringBuilder();
    private final List<Object> params = new ArrayList<>();
    public SafeSqlBuilder update(String table){
        sql.append(UPDATE).append(table);
        return this;
    }
    public SafeSqlBuilder set(String column,Object value){
        if(sql.indexOf(SET) == -1){
            sql.append(SET_FLAG);
        }else{
            sql.append(COMMA);
        }
        sql.append(column).append(" = ?");
        params.add(value);
        return this;
    }
    public SafeSqlBuilder setExpression(String column,String expression){
        if(sql.indexOf(SET) == -1){
            sql.append(SET_FLAG);
        }else{
            sql.append(COMMA);
        }
        sql.append(column).append(" = ").append(expression);
        return this;
    }
    public SafeSqlBuilder where(String condition,Object value){
        sql.append(WHERE).append(condition);
        params.add(value);
        return this;
    }
    public SafeSqlBuilder and(String condition,Object value){
        sql.append(AND).append(condition);
        params.add(value);
        return this;
    }
    public SafeSqlBuilder in(String column, Collection<?> value){
        sql.append(AND).append(column).append(" in (");
        String ins = value.stream().map(n-> QUESTION).collect(Collectors.joining(COMMA));
        sql.append(ins).append(RIGHT);
        params.addAll(value);
        return this;
    }
    public String getSql(){
        return sql.toString();
    }
    public Object[] getParams(){
        return params.toArray();
    }
}
